package practise;

import java.util.Stack;

class MinStack {
    private Stack<Integer> stack;
    private Stack<Integer> minStack;
    public MinStack() {
        this.stack = new Stack<>();
        this.minStack = new Stack<>();
    }
    
    public void push(int val) {
        stack.push(val);
        if(minStack.isEmpty() || minStack.peek() >= val) {
            minStack.push(val);
        }
    }
    
    public void pop() {
        if(!minStack.isEmpty() && !stack.isEmpty()) {
            if(stack.pop().equals(minStack.peek())) {
                minStack.pop();
            }
        }
    }
    
    public int top() {
        return !stack.isEmpty() ? stack.peek() : 0;
    }
    
    public int getMin() {
        return !minStack.isEmpty() ? minStack.peek() : 0;
    }
}
